import Vue from 'vue'
import Vuerouter from 'vue-router'

import pathArr from '@/router/pathArr.js'

import Login from '@/components/MyLogin.vue'
import Home from '@/components/MyHome.vue'

import Users from '@/components/menus/MyUsers.vue'
import Rights from '@/components/menus/MyRights.vue'
import Goods from '@/components/menus/MyGoods.vue'
import Orders from '@/components/menus/MyOrders.vue'
import Settings from '@/components/menus/MySettings.vue'
import UserDetails from '@/components/user/MyUserDetail.vue'



Vue.use(Vuerouter)

const router = new Vuerouter({
  routes:[
    // 重定向
    {path:'/',component:Login},
    // 登录的路由规则
    {path:'/login',component:Login},
    {path:'/home',component:Home,redirect:'/home/users',children:[
      {path:'users',component:Users},
      {path:'rights',component:Rights},
      {path:'goods',component:Goods},
      {path:'orders',component:Orders},
      {path:'settings',component:Settings},
      // 用户详情页的路由规则
      {path:'userinfo/:id',component:UserDetails, props:true}
    ]}
  ]
})
// 声明全局前置守卫
router.beforeEach(function(to, from, next) {
  if (pathArr.indexOf(to.path) != -1) {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})
export default router